<?php
/**
 * @title 264. 丑数 II - 三指针
 * @author start2004
 */

class Solution {

    /**
     * @param Integer $n
     * @param Integer[] $primes
     * @return Integer
     */
    function nthUglyNumber($n) {
        /**
         * @since 2020-11-13 3指针
         */
        $p2 = $p3 = $p5 = 0;

        /**
         * @since 2020-11-13 丑数
         */
        $nums = [1];

        /**
         * @since 2020-11-13 移动指针
         */
        for($i=2; $i<=$n; $i++){
            $num2 = $nums[$p2]*2;
            $num3 = $nums[$p3]*3;
            $num5 = $nums[$p5]*5;

            /**
             * @since 2020-11-13 最小数
             */
            $min = min($num2, $num3, $num5);
            $nums[] = $min;

            /**
             * @since 2020-11-13 移动指针
             */
            if($num2 == $min){
                $p2++;
            } else {}
            if($num3 == $min){
                $p3++;
            } else {}
            if($num5 == $min){
                $p5++;
            } else {}
        }

        /**
         * @return
         */
        return $nums[$n-1];
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.30_11.php
 */
$datas = [
    // [10], // 12
    [11], // 15
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->nthUglyNumber(...$data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
